local skel = fk.CreateSkill {
  name = "emo__anchong",
}

skel:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local existedGeneral = {"moe__shiori"}
    for _, p in ipairs(room.players) do
      table.insertIfNeed(existedGeneral, p.general)
      table.insertIfNeed(existedGeneral, p.deputyGeneral)
    end
    local generalPile = {}
    for _, name in ipairs(room.general_pile) do
      local general = Fk.generals[name]
      if not table.contains(existedGeneral, name) and general.gender == General.Female and general.maxHp == 3 then
        table.insert(generalPile, name)
      end
    end
    if #generalPile == 0 then return false end
    local generals = table.random(generalPile, 5)
    local name = room:askToChooseGeneral(player, {generals = generals, n = 1, no_convert = true, extra_data = {skill_name = skel.name}})
    room:setPlayerMark(player, skel.name, name)
  end,
})

skel:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return player:getMark(skel.name) ~= 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__anchong-change:::"..player:getMark(skel.name)})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = player:getMark(skel.name)
    room:setPlayerMark(player, skel.name, 0)
    data.ignoreDeath = true
    if player.maxHp < 3 then
      room:setPlayerProperty(player, "maxHp", 3)
    end
    if player.hp < 3 then
      room:setPlayerProperty(player, "hp", 3)
    end

    local skills = {}
    local newGeneral = Fk.generals[name]
    for _, skill in ipairs(player.player_skills) do
      if not skill:hasTag(Skill.Compulsory) and skill:isPlayerSkill(player) then
        table.insert(skills, "-"..skill.name)
      end
    end
    for _, n in ipairs(newGeneral:getSkillNameList()) do
      local s = Fk.skills[n]
      local kingdom = s:getSkeleton().attached_kingdom
      if #kingdom == 0 or table.contains(kingdom, newGeneral.kingdom) then
        table.insertIfNeed(skills, n)
      end
    end
    room:changeKingdom(player, newGeneral.kingdom)
    room:sendLog{
      type = "#ChangeHero",
      from = player.id,
      arg = player.general,
      arg2 = name,
      arg3 = "mainGeneral",
    }
    room:setPlayerProperty(player, "general", name)
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.concat(skills, "|"))
    end
  end,
})

Fk:loadTranslationTable{
  ["emo__anchong"] = "暗宠",
  [":emo__anchong"] = "游戏开始时，你从五张体力上限为3的女性武将中选择一个。当你进入濒死状态时，你可以失去所有非锁定技，将体力上限和体力值增加至3，将你替换为选择的武将。",
  ["#emo__anchong-change"] = "暗宠：你可以变身为“%arg”并回复至3血",
}


return skel
